package cn.edu.swu.miniwebsvr.core;

import cn.edu.swu.miniwebsvr.handler.DefaultHandler;
import cn.edu.swu.miniwebsvr.handler.EchoHandler;
import cn.edu.swu.miniwebsvr.handler.TimeHandler;

import java.util.Map;
import java.util.HashMap;


public class Config {
    private final int port = 8080;
    private final String wenRoot = "3-web-server/webroot";
    private final Map<String, Handler> handlerMap = new HashMap<>();

    private Config(){
        handlerMap.put("/",new DefaultHandler());
        handlerMap.put("/time",new TimeHandler());
        handlerMap.put("/echo",new EchoHandler());
    }

    private static class Holder {
        private static final Config INSTANCE = new Config();
    }

    public static Config getInstance() {
        return Holder.INSTANCE;
    }

    public int getPort() {
        return port;
    }

    public String getWenRoot() {
        return wenRoot;
    }

    public Handler getHandler(String url){
        // [222024321072041/王耀东] 实现 URL 精确匹配逻辑
        if (url == null || url.isEmpty()) {
            return handlerMap.get("/");
        }
        String path = url.split("\\?")[0];

        if (handlerMap.containsKey(path)) {

            return handlerMap.get(path);
        }

        return handlerMap.get(url);
    }
}
